<template>
    <div>
        <div class="goods-list">
            <router-link tag="div" :to="'/home/goodslist/' + item.id" v-for="item in goodslist" :key="item.id"
                class="goods-item">
                <img :src="item.img_url" alt="">
                <h3>{{item.title}}</h3>
                <div class="info">
                    <div class="price">
                        <span class="new">￥{{item.sell_price}}</span>
                        <span class="old">￥{{item.market_price}}</span>
                    </div>
                    <div class="sell">
                        <span>热卖中</span>
                        <span>剩{{item.stock_quantity}}件</span>
                    </div>
                </div>
            </router-link>
            
        </div>
        <mt-button type="danger" size="large" style="margin-top: 10px;" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                page: 1,
                goodslist: [],
                isloaded: false
            }
        },
        created() {
            this.getGoodsList()
        },
        methods: {
            async getGoodsList() {
                const {
                    data
                } = await this.$http.get("/api/getgoods?pageindex=" + this.page);
                if (data.status === 0) {
                    if (data.message.length <= 0) {
                        this.isloaded = true;
                    }
                    this.goodslist =  this.goodslist.concat(data.message)
                }
            },
            getMore() {
                // 如果为true，表示数据已经加载完毕了，此时直接return
                if (this.isloaded) return;
                // 点击加载更多的商品
                this.page++;
                this.getGoodsListByPage();
            }
        }
    }
</script>

<style scoped>
    .goods-list {
        display: flex;
        flex-wrap: wrap;
        padding: 0px 7px;
    }

    .goods-item {
        width: 49%;
        margin-top: 7px;
        border: 1px solid #eee;
        padding: 2px;
        box-shadow: 0 0 7px #ccc;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    .goods-item h3 {
        font-size: 14px;
    }

    .goods-item img {
        width: 100%;
        min-height: 170px;
    }

    .info {
        background-color: #eee;
        overflow: hidden;
    }

    .price {
        font-size: 13px;
    }

    .new {
        color: red;
        font-size: 16px;
        font-weight: bold;
        margin-right: 10px;
    }

    .old {
        text-decoration: line-through;
        font-size: 12px;
    }

    .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
        padding: 0px 5px;
    }
</style>